import {
  ImageUrl
} from '../../../utils/imageHelper';
const reqInterface = require("../../../api/models");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    ImageObj: ImageUrl,
    code: '',
    iv: '',
    encrypteddata: '',
    phone: '',
    wxInfo: null,
    showDialog: false,
    showPrivacy: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let self = this;
    if (wx.getStorageSync('wxInfo')) {
      self.setData({
        wxInfo: wx.getStorageSync('wxInfo')
      })
    }
    if (wx.getStorageSync('phone')) {
      self.setData({
        phone: wx.getStorageSync('phone')
      })
    }
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  getUserProfile() {
    let self = this;
    wx.getUserProfile({
      desc: '用于获取用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        self.setData({
          wxInfo: {
            avatar: res.userInfo.avatarUrl,
            nickname: res.userInfo.nickName
          }
        })
        wx.setStorageSync('wxInfo', self.data.wxInfo)
        self.openDialog()
      },
      fail: err => {
        console.log(231, err)
      }
    })
  },

  getPhoneNumber(e) {
    let self = this;
    wx.login({
      success(res) {
        self.setData({
          code: res.code,
        })
        reqInterface.Login({
          jscode: self.data.code
        }).then(res => {
          wx.setStorageSync('token', res.jwttoken.accesstoken);
          self.getPhone(e.detail.code)
        })
      },
      fail(err) {
        console.log(err)

      }
    });
  },

  getPhone(val1) {
    let self = this;
    debugger
    reqInterface.Phone({
      code: val1
    }).then(res => {
      debugger
      self.setData({
        phone: res.phonenumber,
      })
      wx.setStorageSync('phone', res.phonenumber);
    })
  },

  openDialog() {
    let self = this;
    self.setData({
      showDialog: true,
      wxInfo: null
    })
  },



  onChooseAvatar(e) {
    let self = this;
    self.setData({
      wxInfo: {
        avatar: e.detail.avatarUrl,
        nickname: wx.getStorageSync('wxInfo').nickname
      }
    })
    wx.setStorageSync('wxInfo', self.data.wxInfo)
  },

  onSaveAvatarNickname(e) {
    let self = this;
    console.log(e)
    if (!self.data.wxInfo) {
      wx.showToast({
        title: '未编辑信息',
        icon: 'none'
      })
      self.setData({
        showDialog: false,
        wxInfo: wx.getStorageSync('wxInfo')
      })
    }
    if (!e.detail.value.nickname) {
      wx.showToast({
        title: '未编辑昵称',
        icon: 'none'
      })
      self.setData({
        showDialog: false,
        wxInfo: wx.getStorageSync('wxInfo')
      })
    } else {
      self.setData({
        showDialog: false,
        wxInfo: {
          avatar: wx.getStorageSync('wxInfo').avatar,
          nickname: e.detail.value.nickname
        }
      })
      wx.setStorageSync('wxInfo', self.data.wxInfo)
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})